<?php 
	$_SESSION['realpage'] = true;
?>
<!DOCTYPE HTML>
<?php 
require_once "./class.GeneralServices.php";
require_once "./class.User.php";
$error = false;
session_start();
?>
<html>
<head>
<?php require_once "./HtmlIncludes.php";?>
<title>Urban Dynamics</title>
</head>
    <body onload="loadUserProfile();">
    <script type="text/javascript">
		function loadUserProfile()
		{
			var ur = window.parent.document.URL;			
			if(ur.substring(ur.length - 15, ur.length) != "Pages/panes.php")
				window.location.replace("./panes.php");
			LoadUserFriends(); 
			LoadUserWBFriends();
			LoadUserBannedFriends();
		}
    </script>
    	<div class="PaneHeader">
    		User's Profile    	
    		</div>	    	 	    	    
    	<div id="tabs">
		<ul>
			<li><a href="#tabs-1">Edit Details</a></li>
			<li><a href="#tabs-2">User's Friends</a></li>
		</ul>
		<div id="tabs-1">			
			<div id="UserDetailsformDiv">
				<form id="UserDetailsForm" action="" METHOD=POST>
					<input type="hidden" name="argler" value="<?php echo $spoof_preventer ?>"/>
					<input type="hidden" id="uid" value="<?php echo $_SESSION['userObj']->uid;?>"/>
					<fieldset>
						<label for="Firstname">First name</label>
						<br>
						<input type="text" name="Firstname" id="Firstname" value="<?php if(isset($_SESSION['userObj'])){echo $_SESSION['userObj']->FirstName;}?>" class="text ui-widget-content ui-corner-all" />	
						<br>
						<label for="Lastname">Last name</label>
						<br>
						<input type="text" name="Lastname" id="Lastname" value="<?php if(isset($_SESSION['userObj'])){echo $_SESSION['userObj']->Surname;}?>" class="text ui-widget-content ui-corner-all" />
						<br>						
						<label for="Email">Email</label>
						<br>
						<input type="text" name="Email" id="Email" value="<?php if(isset($_SESSION['userObj'])){echo $_SESSION['userObj']->Email;}?>" class="text ui-widget-content ui-corner-all" />	
						<br>
						<label for="Birthday">Birthday</label>
						<br>
						<input type="text" name="Birthday" id="Birthday" value="<?php if(isset($_SESSION['userObj'])){echo $_SESSION['userObj']->birthday;}?>" class="text ui-widget-content ui-corner-all" />
						<br>
						<label for="About">About</label>
						<br>
						<textarea cols="16" rows="3" name="about" id="About" class="text ui-widget-content ui-corner-all"><?php if(isset($_SESSION['userObj'])){echo str_replace('<br>', "\n", $_SESSION['userObj']->About);}?></textarea>	
					</fieldset>
					<button id="UpdateUserDetail" style="float: right;" onclick="UpdateUserDetails()">Update</button>
				</form>
				</div>		
		    </div>
			<div id="tabs-2">
				<span>People who are your friends:</span><br>
		    	<div id="userFriendsDiv">
		    		
		    	</div>
		    	<br><span>People who want to be your friends:</span><br>
		    	<div id="userWanabeFriendsDiv">
		    		
		    	</div>
		    	<!-- <br><span>People you banned:</span><br>
		    	<div id="userBannedFriendsDiv">
		    		
		    	</div> -->
			</div>
		</div>    
	</body>  
<script type="text/javascript">
$(function() {
	$( "#tabs" ).tabs();
});
$('#UpdateUserDetail').button();
$('#Birthday').datepicker({changeMonth: true, changeYear: true, maxDate: new Date(), yearRange: '1900:2012'});

function UpdateUserDetails()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==0)
    {
	  if(xmlhttp.responseText == 'false')
	  {
			alert("update failed!");
	  }
	  else
	  {
		  window.parent.document.getElementById('iFrame').contentWindow.location.reload();
	  }
    }
  };
fname = $('#Firstname').val();
lanme = $('#Lastname').val();
email = $('#Email').val();
bday = $('#Birthday').val();
about = replaceBR($('#About').val());
xmlhttp.open("GET","./ajax.php?method=UpdateUserDetails&sid="+"<?php echo session_id(); ?>"+"&fname="+fname+"&lname="+lanme+"&email="+email+"&bday="+bday+"&about="+about,true);
xmlhttp.send();
}

function LoadUserFriends()
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
		    if(xmlhttp.responseText == 'false')
		    {
				alert('false');
		    }
		    else
		    {
		    	$('#userFriendsDiv').append(xmlhttp.responseText);
		    }
		}
	};
	xmlhttp.open("GET","./ajax.php?method=LoadUserFriends&sid="+"<?php echo session_id(); ?>",true);
	xmlhttp.send();
	return true;
}

function LoadUserWBFriends()
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp3=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp3.onreadystatechange=function()
	{
		if (xmlhttp3.readyState==4 && xmlhttp3.status==200)
	    {
		    if(xmlhttp3.responseText == 'false')
		    {
				alert('false');
		    }
		    else
		    {
		    	$('#userWanabeFriendsDiv').append(xmlhttp3.responseText);
		    }
		}
	};
	xmlhttp3.open("GET","./ajax.php?method=LoadUserWBFriends&sid="+"<?php echo session_id(); ?>",true);
	xmlhttp3.send();
	return true;
}

function LoadUserBannedFriends()
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp5=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp5=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp5.onreadystatechange=function()
	{
		if (xmlhttp5.readyState==4 && xmlhttp5.status==200)
	    {
		    if(xmlhttp5.responseText == 'false')
		    {
				alert('false');
		    }
		    else
		    {
		    	$('#userBannedFriendsDiv').append(xmlhttp5.responseText);
		    }
		}
	};
	xmlhttp5.open("GET","./ajax.php?method=LoadUserBannedFriends&sid="+"<?php echo session_id(); ?>",true);
	xmlhttp5.send();
	return true;
}

function setFriendId(fid)
{
	if(fid != null)
	{
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
			    if(xmlhttp.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
			    	setGroupId(fid);
			    }
			}
		};
		xmlhttp.open("GET","./ajax.php?method=setFid&sid="+"<?php echo session_id(); ?>"+"&fid="+fid,true);
		xmlhttp.send();
	}
}

function setGroupId(gid)
{
	if(gid != null)
	{
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
			    if(xmlhttp.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
			    	ShowGroupTab(gid);
			    }
			}
		};
		xmlhttp.open("GET","./ajax.php?method=setGid&sid="+"<?php echo session_id(); ?>"+"&gid="+gid,true);
		xmlhttp.send();
	}
}

function InviteToGroup(uid, val)
{        		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp4=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp4=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp4.onreadystatechange=function()
		{
			if (xmlhttp4.readyState==4 && xmlhttp4.status==200)
		    {
			    if(xmlhttp4.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
				    if(val == "true")
				    {
						$("#"+uid+"invite").css("display", "none");
						$("#"+uid+"invite").css('visibility', "hidden");

						$("#"+uid+"uninvite").css('display', 'block');
						$("#"+uid+"uninvite").css('visibility', 'visible');
				    }
				    else
				    {
				    	$("#"+uid+"uninvite").css('display', 'none');
						$("#"+uid+"uninvite").css('visibility', 'hidden');

						$("#"+uid+"invite").css('display', 'block');
						$("#"+uid+"invite").css('visibility', 'visible');
				    }        					    
			    }
			}
		};
		xmlhttp4.open("GET","./ajax.php?method=inviteToGroup&sid="+"<?php echo session_id(); ?>"+"&uid="+uid+"&val="+val,true);
		xmlhttp4.send();
}

function FriendUnFriend(uid, val)
{        		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpFriendUnFriend=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpFriendUnFriend=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpFriendUnFriend.onreadystatechange=function()
		{
			if (xmlhttpFriendUnFriend.readyState==4 && xmlhttpFriendUnFriend.status==200)
		    {
			    if(xmlhttpFriendUnFriend.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
				    if(val == "true")
				    {
						$("#"+uid+"invite").css("display", "none");
						$("#"+uid+"invite").css('visibility', "hidden");

						$("#"+uid+"uninvite").css('display', 'block');
						$("#"+uid+"uninvite").css('visibility', 'visible');
				    }
				    else
				    {
				    	$("#"+uid+"uninvite").css('display', 'none');
						$("#"+uid+"uninvite").css('visibility', 'hidden');

						$("#"+uid+"invite").css('display', 'block');
						$("#"+uid+"invite").css('visibility', 'visible');
				    }        					    
			    }
			}
		};
		xmlhttpFriendUnFriend.open("GET","./ajax.php?method=FriendUnFriend&sid="+"<?php echo session_id(); ?>"+"&uid="+uid+"&val="+val,true);
		xmlhttpFriendUnFriend.send();
}
</script>  
</html>
<?php 
	$_SESSION['realpage'] = false;
?>